﻿//-------------------------------------------------------------------
//	Copyright (c) 2012 Fedotov I.V.
//	All rights reserved.
//-------------------------------------------------------------------
/*
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program. If not, see http://www.gnu.org/licenses/.

	Project SVN page 
*/

#ifndef __FILECONTAINER_H__
#define __FILECONTAINER_H__

#include <string>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <vector>

#include "../sptr.h"
#include "../uniq_id.h"

#include "../misc/eventsender.h"
#include "../misc/eventlistener.h"

using namespace std;

typedef enum ContainerType{
 CT_INVALID=-1,
 CT_CONTAINER=0,

 CT_USTRING,
 CT_LANG_PAIR_USTRING,
 CT_LANG_SELECTOR,
 CT_LANG_USTRING,
 CT_LANGUAGES,
 CT_POSITION,
 CT_SIZE_XY,
 CT_LIMIT,
 CT_TRANSFORMATION,
 //MACROS015:
 CT_ID_LIST_USTRING,
 CT_ID_LIST_LANG_PAIR_USTRING,
 CT_ID_LIST_LANG_SELECTOR,
 CT_ID_LIST_LANG_USTRING,
 CT_ID_LIST_LANGUAGES,
 CT_ID_LIST_POSITION,
 CT_ID_LIST_SIZE_XY,
 CT_ID_LIST_LIMIT,
 CT_ID_LIST_TRANSFORMATION
 //MACROS016:
};

typedef enum FileContainerFlags{
 FC_FLAG_READ_ONLY=1
};

class FileContainer:public UniqID{

private:

  wstring fileName;
	long fileCurPosition;
	long fileSize;

	ContainerType containerType;
	//__int64 containerID;
	long containerSize;
	long dataSize;
	char *containerData;
	//Loaded version.
	//Version for save allways are high version
	//Version for save=1
	long containerVersion;
	long containerVersionForSave;

	long headerSize;

	unsigned long containerFlags;
	//Название для контейнера (аналог папки)
	//Не является обязательным, служит для упрощения понимания что где в редакторе
	//Сохраняется и считывается для CT_CONTAINER
  wstring containerCaption;

	vector<SPtr<FileContainer>> childs;
	vector<SPtr<int>> ipvect;
	vector<int> *ivect;
	
	long parse(FILE *_stream,long _fileCurPosition,long _fileSize,long _containerVersion=-1);
	////Только для рассчета размера контейнера (CT_CONTAINER)
	//void calcContainerSize();

  void init();

public:

	FileContainer();
	FileContainer(ContainerType ct);
	~FileContainer();

	int load();
	//рекурсивно записывает контейнер и все его подконтейнеры
	int save();

	//абвгд
	//Для внутреннего использования
	int saveThis(FILE *_stream);

	int setFileName(wstring &_fileName);
	int setFileName(wchar_t *_fileName);
	//int SaveAs(string &_fileName);
	void setContainerCaption(wstring _caption);
	void setContainerCaption(wchar_t *_caption);
	wstring getContainerCaption();

	long getContainerSize();
	long getDataSize();

	ContainerType getContainerType();
	long getContainerVersion();
	long getContainerVersionForSave();
	char *getContainerData();
	unsigned long getContainerFlags();
	vector<SPtr<FileContainer>> *getChilds();

	void setContainerType(ContainerType ct);
	int setDataCopy(char *inData=NULL,long _size=0);

	//int copyDataToContainerData(char *inData,unsigned long size);

  FileContainer *getContainerAtID(__int64 id);

	int addFileContainerToChilds(SPtr<FileContainer> &_fc);
	
};


#endif